import WebSocket, { WebSocketServer } from 'ws';
//引入
const wss = new WebSocketServer({ port: 8080 });
//设置端口


//创造连接
wss.on('connection', function connection(ws) {
    //ws表示链接的用户
  ws.on('error', console.error);

   //链接的用户发送来消息
  ws.on('message', function message(data, isBinary) {
    wss.clients.forEach(function each(client) {
        //循环所有人
      if (client.readyState === WebSocket.OPEN) {
         //发送消息
        client.send(data, { binary: isBinary });
      }
    });
  });
});